package com.jn.gsondemo.views;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jn.gsondemo.R;
import com.jn.gsondemo.base.BaseActivity;
import com.jn.gsondemo.entity.Book;
import com.jn.gsondemo.entity.Olympic;
import com.jn.gsondemo.utils.FileUtils;

import java.util.List;

public class OlympicActivity extends BaseActivity {

    private List<Olympic.ResultMedalsList> medalsList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_olympic;
    }

    @Override
    public void init() {
        findViewById(R.id.iv_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        String fromAssets = FileUtils.getFromAssets(this, "olympic.json");
        System.out.println(fromAssets);
        Gson gson = new Gson();
        Olympic olympic = gson.fromJson(fromAssets, Olympic.class);
        medalsList = olympic.result.medalsList;
        System.out.println("===========" + olympic.msg);
        RecyclerView recyclerview = findViewById(R.id.recyclerview);

        MyAdapter mMyAdapter = new MyAdapter();
        recyclerview.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(OlympicActivity.this);
        recyclerview.setLayoutManager(layoutManager);
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        //绑定item布局
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(OlympicActivity.this, R.layout.item_layout, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        //给item添加数据
        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            Olympic.ResultMedalsList resultMedalsList = medalsList.get(position);
            holder.title.setText("国家：" + resultMedalsList.countryname + "" + resultMedalsList.countryid);
            StringBuilder sb = new StringBuilder();
            sb.append("排名：").append(resultMedalsList.rank).append("\n");
            sb.append("奖牌总数量：").append(resultMedalsList.count).append("\n");
            sb.append("金牌数量：").append(resultMedalsList.gold).append("\n");
            sb.append("银牌数量：").append(resultMedalsList.silver).append("\n");
            sb.append("铜牌数量：").append(resultMedalsList.bronze).append("\n");
            holder.content.setText(sb.toString());
        }

        //设置item条数
        @Override
        public int getItemCount() {
            return medalsList == null ? 0 : medalsList.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView title;
        TextView content;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.textView2);
            content = itemView.findViewById(R.id.textView3);
        }
    }
}